home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / make / rules.c < prev   
C/C++ Source or Header  |  1990-07-15  |  10KB  |  326 lines

  1. /*************************************************************************
  2.  *
  3.  *  m a k e :   r u l e s . c
  4.  *
  5.  *  Control of the implicit suffix rules
  6.  *========================================================================
  7.  * Edition history
  8.  *
  9.  *  #    Date                         Comments                       By
  10.  * --- -------- ---------------------------------------------------- ---
  11.  *   1    ??                                                         ??
  12.  *   2 01.07.89 $<,$* bugs fixed, impl. r. ending in expl. r. added  RAL
  13.  *   3 23.08.89 suffix as macro, testname intr., algorithem to find
  14.  *              source dep. made more intelligent (see Readme3)      RAL
  15.  *   4 30.08.89 indention changed                                    PSH,RAL
  16.  *   5 03.09.89 fixed LZ eliminated                                  RAL
  17.  *   6 07.09.89 rules of type '.c', .DEFAULT added, dep. search impr.RAL
  18.  * ------------ Version 2.0 released ------------------------------- RAL
  19.  *
  20.  *************************************************************************/
  21.  
  22. #include "h.h"
  23.  
  24.  
  25. /*
  26.  *    Dynamic dependency.  This routine applies the suffis rules
  27.  *    to try and find a source and a set of rules for a missing
  28.  *    target.  If found, np is made into a target with the implicit
  29.  *    source name, and rules.  Returns TRUE if np was made into
  30.  *    a target.
  31.  */
  32. bool dyndep(np,pbasename,pinputname)
  33. struct name  *np;
  34. char        **pbasename;        /*  Name without suffix  */
  35. char        **pinputname;
  36. {
  37.   register char *p;
  38.   register char *q;
  39.   register char *suff;                /*  Old suffix  */
  40.   struct name   *op = (struct name *)0,*optmp;    /*  New dependent  */
  41.   struct name   *sp;                /*  Suffix  */
  42.   struct line   *lp,*nlp;
  43.   struct depend *dp,*ndp;
  44.   struct cmd    *cmdp;
  45.   char          *newsuff;
  46.   bool           depexists = FALSE;
  47.  
  48.  
  49.   p = str1;
  50.   q = np->n_name;
  51.   suff = suffix(q);
  52.   while (*q && (q < suff || !suff)) *p++ = *q++;
  53.   *p = '\0';
  54.   if ((*pbasename = malloc(strlen(str1)+1)) == (char *)0 )
  55.      fatal("No memory for basename",(char *)0,0);
  56.   strcpy(*pbasename,str1);
  57.   if ( !suff) suff = p - str1 + *pbasename;  /* set suffix to nullstring */
  58.  
  59.   if (!((sp = newname(".SUFFIXES"))->n_flag & N_TARG))  return FALSE;
  60.  
  61.   /* search all .SUFFIXES lines */
  62.   for (lp = sp->n_line; lp; lp = lp->l_next)
  63.      /* try all suffixes */
  64.      for (dp = lp->l_dep; dp; dp = dp->d_next) {
  65.         /* compose implicit rule name (.c.o)...*/
  66.         newsuff = dp->d_name->n_name;
  67.         while (strlen(suff)+strlen(newsuff)+1 >= str1s.len) strrealloc(&str1s);
  68.         p = str1;
  69.         q = newsuff;
  70.         while (*p++ = *q++) ;
  71.         p--;
  72.         q = suff;
  73.         while (*p++ = *q++) ;
  74.         /* look if the rule exists */
  75.         sp = newname(str1);
  76.         if (sp->n_flag & N_TARG) {
  77.            /* compose resulting dependency name */
  78.            while (strlen(*pbasename) + strlen(newsuff)+1 >= str1s.len)
  79.               strrealloc(&str1s);
  80.            q = *pbasename;
  81.            p = str1;
  82.            while (*p++ = *q++) ;
  83.            p--;
  84.            q = newsuff;
  85.            while (*p++ = *q++) ;
  86.            /* test if dependency file or an explicit rule exists */
  87.            if ((optmp= testname(str1)) != (struct name *)0) {
  88.               /* store first possible dependency as default */
  89.               if ( op == (struct name *)0) {
  90.                  op = optmp;
  91.                  cmdp = sp->n_line->l_cmd;
  92.               }
  93.               /* check if testname is an explicit dependency */
  94.               for ( nlp=np->n_line; nlp; nlp=nlp->l_next) {
  95.                  for( ndp=nlp->l_dep; ndp; ndp=ndp->d_next) {
  96.                     if ( strcmp( ndp->d_name->n_name, str1) == 0) {
  97.                        op = optmp;
  98.                        cmdp = sp->n_line->l_cmd;
  99.                        ndp = (struct depend *) 0;
  100.                        goto found2;
  101.                     }
  102.                     depexists = TRUE;
  103.                  }
  104.               }
  105.               /* if no explicit dependencies : accept testname */
  106.               if (!depexists)  goto found;
  107.            }
  108.         }
  109.      }
  110.  
  111.   if ( op == (struct name *)0) {
  112.      if( np->n_flag & N_TARG) {     /* DEFAULT handling */
  113.         if (!((sp = newname(".DEFAULT"))->n_flag & N_TARG))  return FALSE;
  114.         if (!(sp->n_line)) return FALSE;
  115.         cmdp = sp->n_line->l_cmd;
  116.         for ( nlp=np->n_line; nlp; nlp=nlp->l_next) {
  117.            if ( ndp=nlp->l_dep) {
  118.               op = ndp->d_name;
  119.               ndp = (struct depend *)0;
  120.               goto found2;
  121.            }
  122.         }
  123.         newline(np, (struct depend *)0, cmdp, 0);
  124.         *pinputname = (char *)0;
  125.         *pbasename  = (char *)0;
  126.         return TRUE;
  127.      }
  128.   else return FALSE;
  129.   }
  130.  
  131. found:
  132.   ndp = newdep(op, (struct depend *)0);
  133. found2:
  134.   newline(np, ndp, cmdp, 0);
  135.   *pinputname = op->n_name;
  136.   return TRUE;
  137. }
  138.  
  139.  
  140. /*
  141.  *    Make the default rules
  142.  */
  143. void makerules()
  144. {
  145.   struct cmd    *cp;
  146.   struct name   *np;
  147.   struct depend *dp;
  148.  
  149.  
  150. #ifdef eon
  151.   setmacro("BDSCC", "asm");
  152.   /*    setmacro("BDSCFLAGS", "");    */
  153.   cp = newcmd("$(BDSCC) $(BDSCFLAGS) -n $<", (struct cmd *)0);
  154.   np = newname(".c.o");
  155.   newline(np, (struct depend *)0, cp, 0);
  156.  
  157.   setmacro("CC", "c");
  158.   setmacro("CFLAGS", "-O");
  159.   cp = newcmd("$(CC) $(CFLAGS) -c $<", (struct cmd *)0);
  160.   np = newname(".c.obj");
  161.   newline(np, (struct depend *)0, cp, 0);
  162.  
  163.   setmacro("M80", "asm -n");
  164.   /*    setmacro("M80FLAGS", "");    */
  165.   cp = newcmd("$(M80) $(M80FLAGS) $<", (struct cmd *)0);
  166.   np = newname(".mac.o");
  167.   newline(np, (struct depend *)0, cp, 0);
  168.  
  169.   setmacro("AS", "zas");
  170.   /*    setmacro("ASFLAGS", "");    */
  171.   cp = newcmd("$(ZAS) $(ASFLAGS) -o $@ $<", (struct cmd *)0);
  172.   np = newname(".as.obj");
  173.   newline(np, (struct depend *)0, cp, 0);
  174.  
  175.   np = newname(".as");
  176.   dp = newdep(np, (struct depend *)0);
  177.   np = newname(".obj");
  178.   dp = newdep(np, dp);
  179.   np = newname(".c");
  180.   dp = newdep(np, dp);
  181.   np = newname(".o");
  182.   dp = newdep(np, dp);
  183.   np = newname(".mac");
  184.   dp = newdep(np, dp);
  185.   np = newname(".SUFFIXES");
  186.   newline(np, dp, (struct cmd *)0, 0);
  187. #endif
  188.  
  189. #ifdef tos
  190. #define unix
  191. #endif
  192.  
  193. /*
  194.  *    Some of the UNIX implicit rules
  195.  */
  196.  
  197. #ifdef unix
  198.  
  199.   setmacro("CC", "cc");
  200.   setmacro("CFLAGS", "-O");
  201. #ifdef MINIXPC
  202.   cp = newcmd("$(CC) -S $(CFLAGS) $<", (struct cmd *)0);
  203.   np = newname(".c.s");
  204. #else
  205.   cp = newcmd("$(CC) -c $(CFLAGS) $<", (struct cmd *)0);
  206.   np = newname(".c.o");
  207. #endif MINIXPC
  208.   newline(np, (struct depend *)0, cp, 0);
  209.  
  210. #ifdef MINIXPC
  211.   cp = newcmd("$(CC) $(CFLAGS) -i -o $@ $<", (struct cmd *)0);
  212. #else
  213.   cp = newcmd("$(CC) $(CFLAGS) -o $@ $<", (struct cmd *)0);
  214. #endif MINIXPC
  215.   np = newname(".c");
  216.   newline(np, (struct depend *)0, cp, 0);
  217.  
  218.   setmacro("AS", "as");
  219.   cp = newcmd("$(AS) -o $@ $<", (struct cmd *)0);
  220.   np = newname(".s.o");
  221.   newline(np, (struct depend *)0, cp, 0);
  222.  
  223.   setmacro("YACC", "yacc");
  224.   /*setmacro("YFLAGS", "");    */
  225.   cp = newcmd("$(YACC) $(YFLAGS) $<", (struct cmd *)0);
  226.   cp = newcmd("mv y.tab.c $@", cp);
  227.   np = newname(".y.c");
  228.   newline(np, (struct depend *)0, cp, 0);
  229.  
  230.   cp = newcmd("$(YACC) $(YFLAGS) $<", (struct cmd *)0);
  231. #ifdef MINIXPC
  232.   cp = newcmd("$(CC) $(CFLAGS) -S y.tab.c", cp);
  233.   cp = newcmd("mv y.tab.s $@", cp);
  234.   np = newname(".y.s");
  235. #else
  236.   cp = newcmd("$(CC) $(CFLAGS) -c y.tab.c", cp);
  237.   cp = newcmd("mv y.tab.o $@", cp);
  238.   np = newname(".y.o");
  239. #endif MINIXPC
  240.   cp = newcmd("rm y.tab.c", cp);
  241.   newline(np, (struct depend *)0, cp, 0);
  242.  
  243.   setmacro("FLEX", "flex");
  244.   cp = newcmd("$(FLEX) $(FLEX_FLAGS) $<", (struct cmd *)0);
  245.   cp = newcmd("mv lex.yy.c $@", cp);
  246.   np = newname(".l.c");
  247.   newline(np, (struct depend *)0, cp, 0);
  248.  
  249.   cp = newcmd("$(FLEX) $(FLEX_FLAGS) $<", (struct cmd *)0);
  250. #ifdef MINIXPC
  251.   cp = newcmd("$(CC) $(CFLAGS) -S lex.yy.s", cp);
  252.   cp = newcmd("mv lex.yy.s $@", cp);
  253.   np = newname(".l.s");
  254. #else
  255.   cp = newcmd("$(CC) $(CFLAGS) -c lex.yy.c", cp);
  256.   cp = newcmd("mv lex.yy.o $@", cp);
  257.   np = newname(".l.o");
  258. #endif MINIXPC
  259.   cp = newcmd("rm lex.yy.c", cp);
  260.   newline(np, (struct depend *)0, cp, 0);
  261.  
  262.   np = newname(".o");
  263.   dp = newdep(np, (struct depend *)0);
  264.   np = newname(".s");
  265.   dp = newdep(np, dp);
  266.   np = newname(".c");
  267.   dp = newdep(np, dp);
  268.   np = newname(".y");
  269.   dp = newdep(np, dp);
  270.   np = newname(".l");
  271.   dp = newdep(np, dp);
  272.   np